using System;
using System.Collections.Generic;
using System.Text;

namespace Wrath.Xml.Objects.Controls
{
    public class GenericControl : WindowControl, IElementContainer
    {
        private string icon;
        private string iconColor;

        public GenericControl(Element element)
        {
            sourceElement = element;

            Element subElement = element.FindFirstElement("icon", false);
            if (subElement != null)
            {
                icon = subElement.Value.Substring(0);
                element.ChildElements.Remove(subElement);
            }

            subElement = element.FindFirstElement("iconcolor", false);
            if (subElement != null)
            {
                iconColor = subElement.Value.Substring(0);
                element.ChildElements.Remove(subElement);
            }

            base.Parse(sourceElement);
            base.Finalise(sourceElement);            
        }

        #region IElementContainer

        private Element sourceElement;
        public Element SourceElement
        {
            get { return sourceElement; }
        }

        public void Commit()
        {
            sourceElement = new Element("genericcontrol");

            if (!String.IsNullOrEmpty(icon))
            {
                Element element = new Element("icon");
                element.Value = icon;

                sourceElement.ChildElements.Add(element);
            }

            if (!String.IsNullOrEmpty(iconColor))
            {
                Element element = new Element("iconcolor");
                element.Value = iconColor;

                sourceElement.ChildElements.Add(element);
            }

            base.Commit(sourceElement);
            base.CommitFinalize(sourceElement);            
        }

        #endregion
    }
}
